﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Channels;
using YanShu.Entity;
using YanShu.Utils;

namespace YanShu.Handler
{
    public class ProxyIdleCheckHandler : IdleStateHandler
    {
        public static int USER_CHANNEL_READ_IDLE_TIME = 1200;

        public static int READ_IDLE_TIME = 60;

        public static int WRITE_IDLE_TIME = 40;

        //写法太特殊，暂时不解
        public ProxyIdleCheckHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) : base(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds)
        {
        }

        protected override void ChannelIdle(IChannelHandlerContext context, IdleStateEvent stateEvent)
        {
            if (IdleStateEvent.FirstWriterIdleStateEvent == stateEvent)
            {
                ProxyMessage proxyMessage = new ProxyMessage();
                proxyMessage.SetType(ProxyMessage.TYPE_HEARTBEAT);
                context.Channel.WriteAndFlushAsync(proxyMessage);
                MainWindow.SetText("发送心跳检测");
            }
            else if (IdleStateEvent.FirstReaderIdleStateEvent == stateEvent)
            {
                context.CloseAsync();
            }
            base.ChannelIdle(context, stateEvent);
        }

    }
}
